package com.ymk.cmd;

import com.alibaba.fastjson.JSON;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.code.req.ReqCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.mq.MessageSender;
import com.ymk.service.IGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class GoodsGetCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        String goodsStr = this.redisTemplate.opsForValue().get(SysConstants.REDIS_KEY_GOODS + reqData.getId());
        if (StringUtils.isNotBlank(goodsStr)) {
            respSign.setSource(goodsStr);
            return respSign;
        }

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .setReturnStruct(sign.getReturnStruct());
        GoodsBO respData = this.goodsService.get(paramsBuilder.build());
        respSign.setSource(super.getSource(respData));

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("loadSku", true)
                .setReturnStruct(sign.getReturnStruct());
        GoodsBO respData = this.goodsService.get(paramsBuilder.build());
        respSign.setSource(super.getSource(respData));

        return respSign;
    }

    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);
        String goodsBOStr = this.redisTemplate.opsForValue().get(SysConstants.REDIS_KEY_GOODS + reqData.getId());
        if (StringUtils.isNotBlank(goodsBOStr)) {
            respSign.setSource(super.getSource(JSON.parseObject(goodsBOStr,GoodsBO.class)));
            return respSign;
        }
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getId())
                .addParam("loadCat", true)
                .addParam("loadSku", true)
                .setReturnStruct(sign.getReturnStruct());

        GoodsBO respData = this.goodsService.get(paramsBuilder.build());

        this.messageSender.sendGoodsCacheSync(reqData.getId(), sign);
        respSign.setSource(super.getSource(respData));
        return respSign;
    }

    @Override
    public CmdSign executeServer(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        Map param = super.getData(sign, Map.class);

        GoodsBO respData = this.goodsService.get(param);
        respSign.setSource(super.getSource(respData));

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        GoodsBO reqData = super.getData(sign, GoodsBO.class);
        if (Objects.isNull(reqData.getId())) {
            throw new MallException("id为空");
        }

        return null;
    }

    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_GET;
    }

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private MessageSender messageSender;
}